<template>
    <div class="columns py-6">
        <div class="column is-half is-offset-one-quarter">
            <el-card shadow="never">
                <div slot="header" class="has-text-centered has-text-weight-bold">
                    新用户入驻
                </div>
                <div>
                    <el-form v-loading="loading" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                        <el-form-item label="账户" prop="name">
                            <el-input type="text" v-model="ruleForm.name"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" prop="password">
                            <el-input v-model="ruleForm.pass" placeholder="请选择输入密码" type="password"></el-input>
                        </el-form-item>
                        <el-form-item label="确认密码" prop="password">
                            <el-input v-model="ruleForm.checkPass" placeholder="请选择输入密码" type="password"></el-input>
                        </el-form-item>
                        <el-form-item label="邮箱" prop="email">
                            <el-input type="email" v-model="ruleForm.email"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="submitForm('ruleForm')">立即注册</el-button>
                            <el-button @click="resetForm('ruleForm')">重置</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </el-card>
        </div>

    </div>
</template>

<script>
    // <!--引入注册api请求 -->
    import {userRegister} from "@/api/auth/auth";

    export default {
        name: "Register",
        data(){
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return{
                //用户注册中，防止用户重新注册，需等待后台回应后再进行下步操作
                loading: false,
                ruleForm: {
                    name: '',
                    pass: '',
                    checkPass: '',
                    email: ''
                } ,
                rules:{
                    name: [
                        { required: true, message: '请输入账户', trigger: 'blur' },
                        { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                    ],
                    pass:  [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { required: true, message: '请再次输入密码', trigger: 'blur' },
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                        { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
                    ]
                }
            }
        },
        methods: {

            submitForm(formName) {
                //校验信息
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.loading = true
                        //服务器校验
                        userRegister(this.ruleForm)
                        .then((value) =>{
                            const {code,message} = value
                            if(code == 200){
                                this.$message({
                                    message: '账号注册成功',
                                    type: 'success'
                                })
                                //账号注册成功后，启动定时器
                                setTimeout(() =>{
                                    this.loading = false
                                    this.$router.push({
                                        path: this.redirect || '/login'
                                    })
                                },0.1*1000)
                            }else{
                                //这里执行不到
                                this.$message.error('注册失败，'+message)
                            }
                        })
                        .catch(()=>{

                        })
                        .finally(()=>{
                            this.loading = false
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>

</style>